package com.dsai.dsai.controller;

import com.dsai.dsai.entity.vo.MessageVO;
import com.dsai.dsai.repository.ChatHistoryRespository;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.messages.Message;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
@RequestMapping("/api/ai")
public class ChatHistoryController {
    private final ChatHistoryRespository chatHistoryRespository;
    private final ChatMemory chatMemory;

    public ChatHistoryController(ChatHistoryRespository chatHistoryRespository, ChatMemory chatMemory) {
        this.chatHistoryRespository = chatHistoryRespository;
        this.chatMemory = chatMemory;
    }
    @GetMapping("/history/{type}")
    public List<String> getChatIds(@PathVariable("type") String type){
        return chatHistoryRespository.getChatIds(type);
    }
    @GetMapping("/history/{type}/{chatId}")
    public List<MessageVO>  getChatHistory(@PathVariable("type") String type, @PathVariable("chatId") String chatId){
        List<Message> messages = chatMemory.get(chatId,Integer.MAX_VALUE);
        if(messages==null) {
            return List.of();
        }
        return messages.stream().map(MessageVO::new).toList();    }
}
